package com.neusoft.neuiotms.dcam.workflow.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.workflow.dto.RukuKJAssetDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.RukuKJDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.RukuKJGoodsFileDTO;
import com.neusoft.neuiotms.dcam.workflow.model.RukuKJ;
import com.neusoft.neuiotms.dcam.workflow.model.RukuKJAsset;
import com.neusoft.neuiotms.dcam.workflow.model.RukuKJGoodsFile;
import com.neusoft.neuiotms.dcam.workflow.repository.RukuKJAssetRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.RukuKJGoodsFileRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.RukuKJRepository;

@Component
public class RukuKJConverter extends AbstractConverter<RukuKJ, RukuKJDTO> {

    @Autowired
    private RukuKJRepository rukuKJRepository;
    @Autowired
    private RukuKJAssetRepository rukuKJAssetRepository;
    @Autowired
    private RukuKJAssetConverter rukuKJAssetConverter;
    @Autowired
    private RukuKJGoodsFileRepository rukuKJGoodsFileRepository;
    @Autowired
    private RukuKJGoodsFileConverter rukuKJGoodsFileConverter;

    @Override
    public RukuKJ toModel(final RukuKJDTO dto) {
        final RukuKJ model;
        if (dto.isNew()) {
            model = new RukuKJ();
        } else {
            model = this.rukuKJRepository.getOne(dto.getId());
        }

        final List<RukuKJAsset> list = model.getAssetList().stream().collect(Collectors.toList());

        // 删除资产信息
        list.forEach(k -> {
            model.getAssetList().remove(k);
            this.rukuKJAssetRepository.delete(k);
        });

        // 配件
        dto.getAssetList().stream().map(k -> {
            final RukuKJAsset asset = this.rukuKJAssetConverter.toModel(k);
            asset.setRuku(model);
            return asset;
        }).filter(s -> null != s).forEach(model.getAssetList()::add);

        final List<RukuKJGoodsFile> goodsList = model.getGoodsFileList().stream().collect(Collectors.toList());

        // 删除配件信息
        goodsList.forEach(k -> {
            model.getGoodsFileList().remove(k);
            this.rukuKJGoodsFileRepository.delete(k);
        });

        // 配件
        dto.getGoodsFileList().stream().map(k -> {
            final RukuKJGoodsFile goodsFile = this.rukuKJGoodsFileConverter.toModel(k);
            goodsFile.setGoods(model);
            return goodsFile;
        }).filter(s -> null != s).forEach(model.getGoodsFileList()::add);

        //合同
        model.setContractCode(dto.getContractCode());
        model.setContractName(dto.getContractName());
        model.setTitle(dto.getTitle());
        model.setRemark(dto.getRemark());
        model.setReceiveDate(dto.getReceiveDate());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setTaskId(dto.getTaskId());
        model.setCreateUser(dto.getCreateUser());
        model.setCreateUserName(dto.getCreateUserName());
        model.setComprehensive(dto.getComprehensive());
        model.setCategories(dto.getCategories());
        model.setLocation(dto.getLocation());
        model.setLocationName(dto.getLocationName());
        model.setAutogenerate(dto.getAutogenerate());

        return model;
    }

    @Override
    public RukuKJDTO toDTO(final RukuKJ model, final boolean forListView) {

        final RukuKJDTO dto = new RukuKJDTO();

        dto.setId(model.getId());
        dto.setContractCode(model.getContractCode());
        dto.setContractName(model.getContractName());
        dto.setId(model.getId());
        dto.setReceiveDate(model.getReceiveDate());
        dto.setRemark(model.getRemark());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setTaskId(model.getTaskId());
        dto.setTitle(model.getTitle());
        dto.setCreateUser(model.getCreateUser());
        dto.setCreateUserName(model.getCreateUserName());
        dto.setComprehensive(model.getComprehensive());
        dto.setCategories(model.getCategories());
        dto.setLocation(model.getLocation());
        dto.setLocationName(model.getLocationName());
        dto.setAutogenerate(model.getAutogenerate());

        if (!forListView) {
            // 资产
            model.getAssetList().stream().map(k -> {
                final RukuKJAssetDTO asset = this.rukuKJAssetConverter.toDTO(k);
                return asset;
            }).filter(s -> null != s).forEach(dto.getAssetList()::add);

            // 配件
            model.getGoodsFileList().stream().map(k -> {
                final RukuKJGoodsFileDTO goodsFile = this.rukuKJGoodsFileConverter.toDTO(k);
                return goodsFile;
            }).filter(s -> null != s).forEach(dto.getGoodsFileList()::add);
        }
        return dto;
    }
}
